﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RelationExpertSystem.Relation;

namespace RelationExpertSystem
{
    class TestPeopleBuilder
    {
        private IEnumerable<Person> _people;
        private IEnumerable<PersonPair> _pairs;

        public event EventHandler<FactsEventArgs> FactsLoaded;
        private void RaiseFactsLoaded(FactsEventArgs args)
        {
            EventHandler<FactsEventArgs> eh = FactsLoaded;
            if (eh != null)
                eh(this, args);
        }

        public void BuildPeople()
        {
            // TODO: Add Ex-Spouse relationships
            Person rosario = new Person("Rosario", Sex.Male);
            Person marie = new Person("Marie", Sex.Female, null, null, rosario);
            //rosario.Spouse = marie;
            Person rich = new Person("Rich", Sex.Male);
            Person roxanne = new Person("Roxanne", Sex.Female, rosario, marie, rich);
            //rich.Spouse = roxanne;
            Person richard = new Person("Richard", Sex.Male);
            Person luke = new Person("Luke", Sex.Male, richard, roxanne);
            Person kristen = new Person("Kristen", Sex.Female, richard, roxanne);
            Person josh = new Person("Josh", Sex.Male, richard, roxanne);
            Person david = new Person("David", Sex.Male);
            Person laysa = new Person("Laysa", Sex.Female, david, roxanne);
            Person liz = new Person("Liz", Sex.Female);
            Person kevin = new Person("Kevin", Sex.Male, rich, liz);
            Person eric = new Person("Eric", Sex.Male, rich, liz);
            Person sharee = new Person("Sharee", Sex.Female, rich, liz);
            Person jennifer = new Person("Jennifer", Sex.Female, rich, liz);
            Person nicole = new Person("Nicole", Sex.Female, rich, liz);
            Person cindy = new Person("Cindy", Sex.Female, rosario, marie);
            Person tony = new Person("Tony", Sex.Male, null, null, cindy);
            //tony.Spouse = cindy;
            Person justin = new Person("Justin", Sex.Male, tony, cindy);
            Person matt = new Person("Matt", Sex.Male, tony, cindy);
            Person theresa = new Person("Theresa", Sex.Female, rosario, marie);
            Person wade = new Person("Wade", Sex.Male, null, null, theresa);
            //theresa.Spouse = wade;
            Person suzie = new Person("Suzie", Sex.Female, rosario, marie);
            Person ashley = new Person("Ashley", Sex.Female, wade, theresa);
            Person bethany = new Person("Bethany", Sex.Female, wade, theresa);

            List<Person> people = new List<Person>();
            people.Add(rosario);
            people.Add(marie);
            people.Add(rich);
            people.Add(roxanne);
            people.Add(richard);
            people.Add(luke);
            people.Add(kristen);
            people.Add(josh);
            people.Add(david);
            people.Add(laysa);
            people.Add(liz);
            people.Add(kevin);
            people.Add(eric);
            people.Add(sharee);
            people.Add(jennifer);
            people.Add(cindy);
            people.Add(tony);
            people.Add(matt);
            people.Add(theresa);
            people.Add(wade);
            people.Add(suzie);
            people.Add(ashley);
            people.Add(bethany);
            _people = people;

            RaiseFactsLoaded(new FactsEventArgs(BuildFactMessages()));
        }

        protected List<string> BuildFactMessages()
        {
            List<string> factMessages = new List<string>();
            foreach (Person p in _people)
            {
                // pseron sex
                factMessages.Add(String.Format("{0} is {1}", p.Name, p.Sex));
                // Person father
                if (!Person.IsNull(p.Father))
                    factMessages.Add(String.Format("{0} is {1}'s {2}", p.Father.Name, p.Name, Father.Instance.GetType().GetShortName()));
                // person mother
                if (!Person.IsNull(p.Mother))
                    factMessages.Add(String.Format("{0} is {1}'s {2}", p.Mother.Name, p.Name, Mother.Instance.GetType().GetShortName()));
                // person spouse
                if (!Person.IsNull(p.Spouse))
                    factMessages.Add(String.Format("{0} is {1}'s {2}", p.Spouse.Name, p.Name, Spouse.Instance.GetType().GetShortName()));
            }

            return factMessages;
        }

        public IEnumerable<Person> GetPeople()
        {
            if (_people == null)
                BuildPeople();

            return _people;
        }

        public void BuildPairs()
        {
            GetPeople();
            List<PersonPair> pairs = new List<PersonPair>();
            foreach (Person p1 in _people)
            {
                foreach (Person p2 in _people)
                {
                    if (p1 == p2)
                        continue;

                    pairs.Add(new PersonPair(p1, p2)); // automatically get pair-up twice; once for each order
                }
            }
            _pairs = pairs;
        }

        public IEnumerable<IRuleTarget<RelationEventArgs>> GetPairs()
        {
            if (_pairs == null)
                BuildPairs();

            return _pairs;
        }

        public void ClearInferences()
        {
            if (_people == null)
                return;

            foreach (Person p in _people)
                p.ClearRelations();
        }
    }
}